﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Transformer.iHunter;
using System.Xml;
namespace Transformer.iHunter.UI
{
    public partial class ChannelCommonSettingsPanel : UserControl
    {
        public ChannelCommonSettingsPanel()
        {
            InitializeComponent();
            string toolTipText = "You can change the last update time of the channel. " +
                "On next synchronization, all messages before \"last update time\" will not be downloaded.";
            toolTip1.SetToolTip(dateTimeLastUpdate, toolTipText);
            toolTip1.SetToolTip(groupBox3, toolTipText);
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public LocalClient Client
        {
            get
            {
                if (DesignMode)
                    return null;
                return folderTreeView.Client;
            }
            set
            {
                if (DesignMode)
                    return;
                folderTreeView.Client = value;
//                this.folderTreeView.SelectedFolderGuid = this.Client.RootFolder.Guid;

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(Client.GlobalSettings);
                XmlElement xmlEle = xmlDoc.DocumentElement["TimeSpan"];//.InnerText;
                if (xmlEle == null)
                {
                    this.LastUpdateTime = DateTime.Now;
                }
                else
                {
                    int days = Int32.Parse(xmlEle.InnerText);
                    this.LastUpdateTime = DateTime.Now.AddDays(-days);
                }
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public string ChannelName
        {
            get
            {
                return this.textBoxName.Text;
            }
            set
            {
                this.textBoxName.Text = value;
            }
        }

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool? CheckExists
        {
            get
            {
                if (DesignMode)
                    return null;

                switch (this.comboBoxCheckExists.SelectedIndex)
                {
                    case 0:
                        return null;
                    case 1:
                        return true;
                    case 2:
                        return false;
                }
                return null;
            }
            set
            {
                if (DesignMode)
                    return;
                if (value.HasValue)
                {
                    if (value.Value)
                    {
                        this.comboBoxCheckExists.SelectedIndex = 1;
                    }
                    else
                    {
                        this.comboBoxCheckExists.SelectedIndex = 2;
                    }
                }
                else
                {
                    this.comboBoxCheckExists.SelectedIndex = 0;
                }
            }
        }

        /// <summary>
        /// Gets or sets the target folder GUID.
        /// </summary>
        /// <value>The target folder GUID.</value>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Guid TargetFolderGuid
        {
            get
            {
                if (DesignMode)
                    return Guid.Empty;

                IFolder selFolder = folderTreeView.GetAndCreateSelectedFolder();
                if (selFolder != null)
                    return selFolder.Guid;
                else
                    return Guid.Empty;
            }
//            set
//            {
//                folderTreeView.SelectedFolderGuid = value;
//            }
        }

        private void ChannelCommonSettingsPanel_Load(object sender, EventArgs e)
        {
            this.comboBoxCheckExists.SelectedIndex = 0;
        }


        public Guid SelectedFolderGuid
        {
            get
            {
                return folderTreeView.SelectedFolderGuid;
            }
            set
            {
                folderTreeView.SelectedFolderGuid = value;
            }
        }

        public DateTime LastUpdateTime
        {
            get
            {
                DateTime time = dateTimeLastUpdate.Value;
                if (time > DateTime.Now)
                {
                    time = DateTime.Now;
                }
                else if (time.Year < 1900)
                {
                    time = time.AddYears(1900 - time.Year);
                }

                return time;
            }

            set
            {
                dateTimeLastUpdate.Value = value;
            }
        }


        public void ResetControlValues(ChannelInfo settings)
        {
            ChannelName = settings.Name;
            SelectedFolderGuid = settings.FolderGuid;
            CheckExists = settings.RefreshExists;
            LastUpdateTime = settings.UpdateTime;
        }
    }
}
